require 'shukujitu.rb'

module TasksHelper

  def today
    @today ||= Time.now.beginning_of_day
  end

  def link_params(options = {})
    [:date, :team, :member, :view].inject(HashWithIndifferentAccess.new) {|hash, key|
      hash[key] = params[key]
      hash
    }.merge(options)
  end

  def day_cell_classes(day, displayed_month)
    classes = ['cell']
    classes << 'saturday' if day.wday == 6
    classes << 'sunday' if day.wday == 0
    classes << 'monday' if day.wday == 1
    classes << 'out-range-day' if day.month != displayed_month.month
    classes << 'holiday' if day.shukujitu?
    if day < today
      classes << 'past'
    elsif day == today
      classes << 'today'
    end
    classes
  end

  # @@task_colors = {}
  def task_classes(task, beginning_of_week)
    classes = ['task']
    begin_column = (task.begin_at - beginning_of_week) / 1.day
    end_column = (task.end_at - beginning_of_week) / 1.day
    classes << (begin_column < 0 ? 'countinue-from' : sprintf('start-at-%d', begin_column))
    classes << 'continue-to' if end_column >= 7
    classes << sprintf('length-%d', [6, end_column].min - [0, begin_column].max + 1)
    # classes << (@@task_colors[task.task.id] ||= cycle('task-1', 'task-2', 'task-3', 'task-4', 'task-5', 'task-6', 'task-7', 'task-8', :name => :task_type))
    classes
  end


  def each_active_days(periods, begin_at, end_at)
    date = begin_at
    periods.each do |period|
      while date <= period.end_at && date <= end_at
        yield date, (date >= period.begin_at)
        date += 1.days
      end
    end
    while date <= end_at
      yield date, false
      date += 1.days
    end
  end

  def each_active_days_in_task(task, begin_at, end_at)
    date = begin_at
    while date < task.begin_at
      yield date, false
      date += 1.days
    end
    while date <= task.end_at && date <= end_at
      yield date, true
      date += 1.days
    end
    while date <= end_at
      yield date, false
      date += 1.days
    end
  end

end
